package test;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Index_test {

	/**
	 * @param args
	 */
	
	static String dirPath = "D:\\Program Files\\eclipse\\workspace\\cs6714\\sampleData1";
	static String indexPath = "D:\\Program Files\\eclipse\\workspace\\cs6714\\indexPath";
	static String LocFile = "D:\\Program Files\\eclipse\\workspace\\cs6714\\sampleLoc1.txt";
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Spatial_Indexer si = new Spatial_Indexer();
		
		
		File INDEX_DIR = new File(indexPath);
		Analyzer a = new StandardAnalyzer(Version.LUCENE_36);
		//open the index and initialize a Searcher object
		Directory dir;
		try {
			dir = FSDirectory.open(INDEX_DIR);
			IndexSearcher searcher = new IndexSearcher(IndexReader.open(dir));
			//create a QueryParser to parser the query string
			QueryParser parser = new QueryParser(Version.LUCENE_36, "content", a);
			//parse the query string to get a Query object, pass it to the 
			//searcher, and output the results
			String query = "food";
			//Query q = null;
			TermQuery tq1 = new TermQuery(new Term("content", "fuel"));
			TermQuery tq2 = new TermQuery(new Term("content", "esso"));
			/*Query q = NumericRangeQuery.newIntRange("content", 201001,
				                                  201012,
				                                  true, true);*/
			BooleanQuery bq = new BooleanQuery();
			bq.add(tq1, Occur.SHOULD);
			bq.add(tq2, Occur.SHOULD);
			/*try {
				q = parser.parse(query);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
			TopDocs tops = searcher.search(bq, 10); // retrieve top-10 results
			for (int i = 0; i < tops.scoreDocs.length; i++) {
				//System.out.println(tops.scoreDocs[i].doc);
				ScoreDoc sdoc = tops.scoreDocs[i];
			    Document doc = searcher.doc(sdoc.doc);
			    //System.out.println("name: "+doc.get("name") + " " + sdoc.score);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
